const {
  NAME_OR_PASSWORD_IS_REQUIRED,
  NAME_HAS_ALREADY_USED,
  USER_DOES_NOT_EXISTS,
  PASSWORD_IS_WRONG,
  ERROR_TOKEN,
} = require("../constants/errtypes");

const errorHandler = (err, ctx) => {
  var message = "",
    status = 0;
  switch (err.message) {
    case NAME_OR_PASSWORD_IS_REQUIRED:
      status = 400;
      message = NAME_OR_PASSWORD_IS_REQUIRED;
      break;
    case NAME_HAS_ALREADY_USED:
      status = 408;
      message = NAME_HAS_ALREADY_USED;
      break;
    case USER_DOES_NOT_EXISTS:
      status = 400;
      message = USER_DOES_NOT_EXISTS;
      break;
    case PASSWORD_IS_WRONG:
      status = 400;
      message = PASSWORD_IS_WRONG;
      break;
    case ERROR_TOKEN:
      status = 400;
      message = ERROR_TOKEN;
      break;
    default:
      status = 404;
      message = "NOT FOUND";
  }
  ctx.status = status;
  ctx.body = message;
};

module.exports = errorHandler;
